import random
import datetime
# products = [
#     ('01',"花生", 3,10),
#     ('02',"瓜子", 3,20),
#     ('03',"矿泉水", 2,30),
#     ('04',"啤酒", 10,20),
#     ('05',"饮料", 4,25),
#     ('06',"八宝粥",5,20)
# ]
# cost = 0
# money = 50
# shopping_cart = []
# print("欢迎来到商场购物！")
#
# while True:
#     print("")
#     print("当前可购买的商品有")
#     for no, item, price, sum in products:
#         print(f"编号:{no} 名称:{item} 价格:{price} 数量:{sum}")
#
#     choice_input = input("你想购买的商品是（输入Q或q退出购物）：")
#     product_info = None
#     if choice_input.upper() == 'Q':
#         print("====小票====")
#         for k_id,k_item,k_price,k_sum in shopping_cart:
#             print(f"您本次购买了{k_item} x {k_sum}:{k_price*k_sum}元")
#             print("花费:",cost,"元")
#             print("剩余:",money,"元")
#         break
#     #param: 获取所选商品信息
#     for no,item,price,sum in products:
#         if choice_input == no:
#             product_info = (no,item,price,sum)
#     if  product_info is None:
#         print("没有这个商品，别瞎弄！")
#         input("输入回车继续")
#         continue
#     tmp = input("请输入你购买数量（输入0不购买本商品）：")
#     try:
#         count_input = int(tmp)
#         tmp_price = product_info[2]
#         if money >= tmp_price*count_input:
#             if product_info is not None:
#                 sum = product_info[3]
#                 if sum >= count_input:
#                     tmp_sum = sum - count_input
#                     tmp_no = product_info[0]
#                     tmp_item = product_info[1]
#                     cart_info = (tmp_no, tmp_item, tmp_price,count_input)
#
#                     if len(shopping_cart) != 0:
#                         for i, (cart_no, cart_item, cart_sum) in enumerate(shopping_cart):
#                             print(cart_no, cart_item, cart_sum)
#                             #param:购物车中有相同物品
#                             if cart_info[0] == cart_no:
#                                 cart_sum += count_input
#                                 cart_info = (cart_no, cart_item,tmp_price ,cart_sum)
#                                 shopping_cart.pop(i)
#                                 shopping_cart.append(cart_info)
#                                 money -= count_input*tmp_price
#                                 cost +=count_input*tmp_price
#                                 print("您花费的马内为：", cost)
#                                 print("您剩余的马内为：", money)
#                                 print("购物车里的东西：", shopping_cart)
#
#                                 for j,(no,item,price,sum) in enumerate(products):
#                                     if no == tmp_no:
#                                         change = (tmp_no, tmp_item, tmp_price, sum - count_input)
#                                         products[j] = change
#                                         break
#                                 break
#                             #param:购物车中没有相同物品
#                             else:
#                                 shopping_cart.append(cart_info)
#                                 money -= count_input * tmp_price
#                                 cost += count_input * tmp_price
#                                 print("您花费的马内为：", cost)
#                                 print("您剩余的马内为：", money)
#                                 print("购物车里的东西：", shopping_cart)
#                                 for j,(no,item,price,sum) in enumerate(products):
#                                     if no == tmp_no:
#                                         change = (tmp_no, tmp_item, tmp_price, sum - count_input)
#                                         products[j] = change
#                                         break
#                                 break
#                     #param：购物车中没有物品
#                     else:
#                         shopping_cart.append(cart_info)
#                         money -= count_input * tmp_price
#                         cost += count_input * tmp_price
#                         print("您花费的马内为：",cost)
#                         print("您剩余的马内为：", money)
#                         for j, (no, item, price, sum) in enumerate(products):
#                             if no == tmp_no:
#                                 change = (tmp_no, tmp_item, product_info[2], sum - count_input)
#                                 products[j] = change
#                                 break
#                         print("购物车里的东西：", shopping_cart)
#
#                 else:
#                     print("剩余商品数量不足")
#                     input("输入回车继续")
#         else:
#             print("穷鬼，钱不够！请到其他超市购买！")
#             continue
#     except ValueError:
#         print("请输入整数")
#         input("输入回车继续")


#param:V2

products = [
    ('01',"花生", 3,10),
    ('02',"瓜子", 3,20),
    ('03',"矿泉水", 2,30),
    ('04',"啤酒", 10,20),
    ('05',"饮料", 4,25),
    ('06',"八宝粥",5,20)
]
discounts = [
    ('01',"花生",0.8),
    ('02',"瓜子",0.4),
    ('03',"矿泉水",0.8),
    ('04',"啤酒",0.95),
    ('05',"饮料",0.9),
    ('06',"八宝粥",0.8),
    ('07','无折扣',1)
]
cost = 0
money = 50
shopping_cart = []
a = random.randint(0,6)
print("欢迎来到商场购物！")
discount = discounts[a]
if a == 6:
    print("您没有抽到折扣卷")
else:
    print(f"您抽到了{discount[1]}折扣卷,折扣力度为{discount[2]}")
while True:
    print("")
    print("当前可购买的商品有")
    for no, item, price, sum in products:
        if no == discount[0]:
            dis_price = '{:.2f}'.format(price*discount[2])
            print(f"编号:{no} 名称:{item} 折扣后价格:{dis_price} 数量:{sum}")
        else:
            print(f"编号:{no} 名称:{item} 价格:{price} 数量:{sum}")

    choice_input = input("你想购买的商品编号是（输入Q或q退出购物,输入T或t退回商品）：")
    product_info = None
    if choice_input.upper() == 'Q':
        print("====小票====")
        for k_id,k_item,k_price,k_sum in shopping_cart:
            print(f"您本次购买了{k_item} x {k_sum}: {k_price*k_sum}元")
            print("花费:",cost,"元")
            print("剩余:",money,"元")
            print("时间:",datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        break
    # param: 获取所选商品信息
    for no,item,price,sum in products:
        if choice_input == no:
            if no == discount[0]:
                product_info = (no,item,price*discount[2],sum)
            else:
                product_info = (no, item, price, sum)

    if choice_input.upper() == "T":
        if len(shopping_cart) == 0:
            print("您的购物车里没有东西")
            continue
        print("购物车里的东西：", shopping_cart)
        back_no = input("请输入您要退回商品的编号(输入0取消该退回):")
        if back_no == "0":
            break
        else:
            t_sum = input("请输入您退回商品的数量:")
            try:
                r_sum = int(t_sum)
                for i,(no,item,price,sum) in enumerate(shopping_cart):
                    print("sum",sum,"r_sum",r_sum)
                    if sum < r_sum:
                        print("您的购物车里没有这么多东西")
                        continue
                    if no == back_no:
                        sum -= r_sum
                        money += r_sum*price
                        print("你的马内剩余:",money)
                        print("退的马内为：",r_sum*price)
                        if sum == 0:
                            shopping_cart.pop(i)
                            print(shopping_cart)
                            continue
            except ValueError:
                print("请输入整数")
                input("输入回车继续")

    if product_info is None and choice_input.upper()!="T" :
        print("没有这个商品，别瞎弄！")
        continue

    if product_info is not None:
        try:
            tmp = input("请输入你购买数量（输入0不购买本商品）：")
            count_input = int(tmp)
            tmp_price = product_info[2]
            if money >= tmp_price * count_input:
                if product_info is not None:
                    sum = product_info[3]
                    if sum >= count_input:
                        tmp_sum = sum - count_input
                        tmp_no = product_info[0]
                        tmp_item = product_info[1]
                        cart_info = (tmp_no, tmp_item, tmp_price, count_input)

                        if len(shopping_cart) != 0:
                            for i, (cart_no, cart_item, cart_sum) in enumerate(shopping_cart):
                                print(cart_no, cart_item, cart_sum)
                                # param:购物车中有相同物品
                                if cart_info[0] == cart_no:
                                    cart_sum += count_input
                                    cart_info = (cart_no, cart_item, tmp_price, cart_sum)
                                    shopping_cart.pop(i)
                                    shopping_cart.append(cart_info)
                                    money -= count_input * tmp_price
                                    cost += count_input * tmp_price
                                    print("您花费的马内为：", cost)
                                    print("您剩余的马内为：", money)
                                    print("购物车里的东西：", shopping_cart)

                                    for j, (no, item, price, sum) in enumerate(products):
                                        if no == tmp_no:
                                            change = (tmp_no, tmp_item, tmp_price, sum - count_input)
                                            products[j] = change
                                            break
                                    break
                                # param:购物车中没有相同物品
                                else:
                                    shopping_cart.append(cart_info)
                                    money -= count_input * tmp_price
                                    cost += count_input * tmp_price
                                    print("您花费的马内为：", cost)
                                    print("您剩余的马内为：", money)
                                    print("购物车里的东西：", shopping_cart)
                                    for j, (no, item, price, sum) in enumerate(products):
                                        if no == tmp_no:
                                            change = (tmp_no, tmp_item, tmp_price, sum - count_input)
                                            products[j] = change
                                            break
                                    break
                        # param：购物车中没有物品
                        else:
                            shopping_cart.append(cart_info)
                            money -= count_input * tmp_price
                            cost += count_input * tmp_price
                            print("您花费的马内为：", cost)
                            print("您剩余的马内为：", money)
                            for j, (no, item, price, sum) in enumerate(products):
                                if no == tmp_no:
                                    change = (tmp_no, tmp_item, product_info[2], sum - count_input)
                                    products[j] = change
                                    break
                            print("购物车里的东西：", shopping_cart)

                    else:
                        print("剩余商品数量不足")
                        input("输入回车继续")
            else:
                print("穷鬼，钱不够！请到其他超市购买！")
                continue
        except ValueError:
            print("请输入整数")
            input("输入回车继续")


